package com.yampi.mahjong.aliapi;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.alipay.sdk.app.PayTask;
import com.unity3d.player.UnityPlayer;

import java.util.Map;

/**
 * Created by 13767 on 2017/10/11.
 */

public class Alipay {
    private static final int SDK_PAY_FLAG = 1;
    private static final String RESULT_SUCCESS = "9000";
    private static final String TIP_PAY_SUCCESS = "支付成功";
    private static final String TIP_PAY_FAILED = "支付失败";

    private void Pay(final String orderInfo)
    {
        Runnable payRunnable = new Runnable()
        {
            @Override
            public void run()
            {
                PayTask alipay = new PayTask(UnityPlayer.currentActivity);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    public void Pay(String app_id, String totalAmount, String body, String rsa2_private, String rsa_private)
    {
        if (TextUtils.isEmpty(app_id) || (TextUtils.isEmpty(rsa2_private) && TextUtils.isEmpty(rsa_private))) {
            new AlertDialog.Builder(UnityPlayer.currentActivity).setTitle("警告").setMessage("需要配置APPID | RSA_PRIVATE")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialoginterface, int i) {
                        }
                    }).show();
            return;
        }
        boolean rsa2 = (rsa2_private.length() > 0);
        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(app_id, totalAmount, body, rsa2);
        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
        String privateKey = rsa2 ? rsa2_private : rsa_private;
        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
        final String orderInfo = orderParam + "&" + sign;
        Pay(orderInfo);
    }

    // 支付结果回调，仅作参考，以服务端确认为准!
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler()
    {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case SDK_PAY_FLAG:
                {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    String resultInfo = payResult.getResult();
                    String resultStatus = payResult.getResultStatus();
                    if (TextUtils.equals(resultStatus, RESULT_SUCCESS))
                    {
                        Toast.makeText(UnityPlayer.currentActivity, TIP_PAY_SUCCESS, Toast.LENGTH_SHORT).show();
                    } else
                    {
                        Toast.makeText(UnityPlayer.currentActivity, TIP_PAY_FAILED, Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    };
}
